import http from './http.js'
import config from '../config/index.js'

export default {
    // 认证页面
    registerWeb(data) {
        const url = `https://gitee.com/oauth/token?grant_type=authorization_code&code=${data.code}&client_id=${config.client_id}&redirect_uri=${data.redirect_uri}&client_secret=${config.client_secret}`
        return http.post(url).then(res => {
            console.log(res);
            if(res) {
                localStorage.setItem('userInfo', JSON.stringify(res))
            }
        })
    },
    // 上传新文件
    uploadNewfile(data) {
        const url = `https://gitee.com/api/v5/repos/${data.owner}/${data.repo}/contents/${data.path}`
        const json = {
            access_token: data.access_token,
            content: data.content,
            message: data.message
        }
        console.log(json);
        return http.post(url, json).then(res => {
            return Promise.resolve(res)
        })
    },
    // 更新文件 PUT
    updateFile(data) {
        const url = `https://gitee.com/api/v5/repos/${data.owner}/${data.repo}/contents/${data.path}`
        const json = {
            access_token: data.access_token,
            sha: data.sha,
            content: data.content,
            message: data.message
        }
        return http.put(url, json).then(res => {
            console.log('OK');
        })
    },
    // 刷新Token
    reGetToken(data) {
        const url = `https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token=${data.refresh_token}`
        return http.post(url).then(res => {
            console.log(res);
            if(res) {
                localStorage.setItem('userInfo', JSON.stringify(res))
            }
            return Promise.resolve(res)
        })
    }
}